import { defineStore } from 'pinia'

const idMap: Map<string, number> = new Map()
const useFastnavStore = defineStore('appfastnav', {
    state: () => {
        const datas: IFastnavItem[] = []
        const currPath: string = ''

        return {
            datas,
            currPath,
        }
    },

    persist: true,

    actions: {


        addData(desc: string, path: string) {
            const data = this.datas.find(item => item.path == path)
            if (data) {
                this.currPath = path
                return
            }

            this.datas.push({ desc, path })

            this.currPath = path
        },

        removeData(path: string): string {
            if (this.datas.length <= 1) return ''

            for (let i = 0; i < this.datas.length; i++) {
                const item = this.datas[i]
                if (item.path != path) continue

                this.datas.splice(i, 1)

                // 关闭时路由id加一，这样它下次打开时就会重新加载
                const id = idMap.get(path) ?? 1
                idMap.set(path, id + 1)

                if (item.path != this.currPath) return ''

                return i == 0 ? this.datas[0].path : this.datas[i - 1].path
            }

            return ''
        },

        // 页签
        idGet(path: string): string {
            const id = idMap.get(path) ?? 1
            console.log('key -----',path + id);
            return path + id
        },

        idAdd(path: string): number {
            const id = (idMap.get(path) ?? 1) + 1
            idMap.set(path, id)
            return id
        },

        idAddAll(datas: IFastnavItem[], excludePath?: string) {
            datas.forEach(item => item.path != excludePath && this.idAdd(item.path))
        },

        isFirst(path: string): boolean {
            return this.datas.length > 0 && this.datas[0].path == path
        },

        isLast(path: string): boolean {
            return this.datas.length > 0 && this.datas[this.datas.length - 1].path == path
        },


        logout() {
            this.$reset()
        },

        // 关闭其他
        removeOther(path: string): string {
            const data = this.datas.find(item => item.path == path)
            if (!data) return ''

            this.idAddAll(this.datas, path)
            this.datas = [data]

            return path == this.currPath ? '' : path
        },

        // 关闭左侧
        removeLeft(path: string): string {
            for (let i = 0; i < this.datas.length; i++) {
                const data = this.datas[i]
                if (data.path != path) continue

                const removes = this.datas.splice(0, i)
                this.idAddAll(removes)

                return path == this.currPath ? '' : path
            }

            return ''
        },

        // 关闭右侧
        removeRight(path: string): string {
            for (let i = 0; i < this.datas.length; i++) {
                const data = this.datas[i]
                if (data.path != path) continue

                const removes = this.datas.splice(i + 1)
                this.idAddAll(removes)

                return path == this.currPath ? '' : path
            }

            return ''
        },


    }
})


export default useFastnavStore
